package novel.front.novellist.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import novel.common.page.PageInfo;
import novel.common.page.PageParam;
import novel.common.utils.redis.JedisUtil;
import novel.front.novellist.entity.LiebaoApp;
import novel.front.novellist.service.LiebaoAppService;
import org.springframework.stereotype.Service;
import novel.front.novellist.dao.LiebaoAppMapper;
import redis.clients.jedis.JedisSentinelPool;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by 888 on 2017/2/13.
 */
@Slf4j
@Service("LiebaoAppService")
public class LiebaoAppServiceImpl implements LiebaoAppService {

    @Resource
    private LiebaoAppMapper liebaoAppMapper;

    @Override
    public void saveAppList(List<LiebaoApp> liebaoAppList) {
        for (LiebaoApp liebaoApp : liebaoAppList) {
            try {
                liebaoAppMapper.insert(liebaoApp);
            } catch (Exception e) {
                log.error("导入{}", e);
                String reg = "[^\u4e00-\u9fa5]";
                String appsum = liebaoApp.getAppSum().replaceAll(reg, "");
                liebaoApp.setAppSum(appsum);
                liebaoAppMapper.insert(liebaoApp);
            }

        }
    }

    @Override
    public void save(LiebaoApp liebaoApp) {
        liebaoAppMapper.insert(liebaoApp);
    }

    @Override
    public void update(LiebaoApp liebaoApp) {
        liebaoAppMapper.updateByPrimaryKeySelective(liebaoApp);
    }

    @Override
    public List<LiebaoApp> searchApp(LiebaoApp liebaoApp, String redisKey) {
//        if (JedisUtil.get(redisKey)!=null){
//            System.out.println("redis输出");
//            return JedisUtil.get(redisKey);
//        }

        List<LiebaoApp> liebaoAppList = liebaoAppMapper.searchApp(liebaoApp);
//        JedisUtil.set(redisKey, liebaoAppList);
        System.out.println("db输出");
        return liebaoAppList;
    }


    @Override
    public List<LiebaoApp> getList(LiebaoApp liebaoApp) {
        List<LiebaoApp> liebaoAppList = liebaoAppMapper.searchApp(liebaoApp);
        return liebaoAppList;
    }

    @Override
    public LiebaoApp getAppByName(String appName) {
        return liebaoAppMapper.getAppByName(appName);
    }

    @Override
    public List<LiebaoApp> getListByCategoryId(int categoryId) {
        return liebaoAppMapper.getListByCategoryId(categoryId);
    }

    @Override
    public List<LiebaoApp> getListDesc(int offset, int limit) {
        return liebaoAppMapper.getListDesc(offset, limit);
    }

    @Override
    public List<LiebaoApp> getListRandom() {
        return liebaoAppMapper.getListRandom();
    }

    @Override
    public PageInfo<LiebaoApp> getListByPage(LiebaoApp liebaoapp, PageParam pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List list = liebaoAppMapper.searchApp(liebaoapp);
        return new PageInfo<LiebaoApp>(list, liebaoapp.getCategoryId());
    }

    @Override
    public LiebaoApp selectByPrimaryKey(Integer id) {
        return liebaoAppMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Map<String, String>> getTagPercent() {
        return liebaoAppMapper.getTagPercent();
    }
}
